package com.atguigu.srb.core.controller.admin;


import com.atguigu.common.exception.BusinessException;
import com.atguigu.common.result.ResponseEnum;
import com.atguigu.common.result.Result;
import com.atguigu.srb.core.pojo.entity.IntegralGrade;
import com.atguigu.srb.core.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 积分等级表 前端控制器
 * </p>
 *
 * @author ZLHao
 * @since 2021-07-12
 */
@Api(tags = "积分等级管理") // 在swagger api文档中会显示名称

@RestController
@RequestMapping("/admin/core/integralGrade")
public class AdminIntegralGradeController {

    @Autowired
    private IntegralGradeService integralGradeService;

    /**
     * @description: 获取积分列表
     * @author Administrator
     * @date 2021/7/13 0013 9:14
     * @version 1.0
     */
    @ApiOperation("积分等级列表") // 在swagger api文档中会显示名称
    @GetMapping("/list")
    public Result listAll(){

        List<IntegralGrade> list = integralGradeService.list();
        return  Result.ok().data("list",list).message("获取列表成功");
    }

    /** 
     * @description: 删除某个积分段 
     * @param: id 
     * @return: boolean 
     * @author Administrator
     * @date: 2021/7/13 0013 9:16
     */
    @ApiOperation("积分删除") // 在swagger api文档中会显示名称
    @DeleteMapping("/remove/{id}")
    public Result deleteById(
            @ApiParam("要删除的ID")
            @PathVariable  Long id){

        boolean b = integralGradeService.removeById(id);
        if (b){
            return Result.ok().message("删除成功");
        }else {
            return Result.error().message("删除失败");
        }


    }

    /**
     * @description: 新增
     * @param: integralGrade
     * @return: com.atguigu.common.result.Result
     * @author Administrator
     * @date: 2021/7/13 0013 11:23
     */
    @ApiOperation("新增积分等级")
    @PostMapping("/save")
    public Result save(
            @ApiParam(value = "积分等级对象",required = true)
            @RequestBody IntegralGrade integralGrade){

        // 判断借款edu是否为空
        if (integralGrade.getBorrowAmount() == null){
            throw new BusinessException(ResponseEnum.BORROW_AMOUNT_NULL_ERROR.getMessage(),ResponseEnum.BORROW_AMOUNT_NULL_ERROR.getCode());
        }


        boolean result = integralGradeService.save(integralGrade);
        if (result){
            return  Result.ok().message("保存成功");
        }else {
            return Result.error().message("保存失败");
        }


    }

    @ApiOperation("根据id查询积分")
    @GetMapping("/get/{id}")
    public Result getById(
            @ApiParam("查询的id")
            @PathVariable Long id){

        IntegralGrade integralGrade = integralGradeService.getById(id);
        if (integralGrade != null){
            return Result.ok().message("查询成功").data("data",integralGrade);
        }else {
            return Result.error().message("查询失败");
        }
    }



    @ApiOperation("更新积分")
    @PutMapping("/update")
    public Result updateById(
            @ApiParam(value = "积分等级对象", required = true)
            @RequestBody IntegralGrade integralGrade){
        boolean b = integralGradeService.updateById(integralGrade);
        if (b){
            return Result.ok().message("更新成功");
        }else {
            return Result.error().message("更新失败");
        }

    }




}

